﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TouchExplorer.Entities;

namespace TouchExplorer.Views
{
    /// <summary>
    /// Interaction logic for ApplicationsBrowser.xaml
    /// </summary>
    public partial class ApplicationsBrowser : UserControl
    {
        public ApplicationsViewModel Model
        {
            get
            {
                return DataContext as ApplicationsViewModel;
            }
        }

        public ApplicationsBrowser()
        {
            InitializeComponent();
            var model = new ApplicationsViewModel();            
            DataContext = model;
            Loaded += new RoutedEventHandler(ApplicationsBrowser_Loaded);
            ListApps.SelectionChanged += new SelectionChangedEventHandler(ListApps_SelectionChanged);
            
        }

        void ContentLoaded(object sender, RoutedEventArgs e)
        {
            ((sender as FrameworkElement).DataContext as RunningApp).Content.UpdateLayout();
            ((sender as FrameworkElement).DataContext as RunningApp).Content.Width++;
            ((sender as FrameworkElement).DataContext as RunningApp).Content.Width--;
        }

        void ApplicationsBrowser_Loaded(object sender, RoutedEventArgs e)
        {
            Model.Load();
            ListApps.UpdateLayout();
        }

        void ListApps_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = ListApps.SelectedItem as RunningApp;
            if (item != null)
            {
                item.Activate();
            }
        }
    }
}
